layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 出库订单预排车表管理
     */
    var OrderOutpreparePlatooncar = {
        tableId: "orderOutpreparePlatooncarTable"
    };

    /**
     * 初始化表格的列
     */
    OrderOutpreparePlatooncar.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'Primary Key'},
            {field: 'storeName', sort: true, title: 'Store Name'},
            {field: 'clientName', sort: true, title: 'Client Name'},
            {field: 'quantity', sort: true, title: 'Request Quantity'},
            {field: 'volume', sort: true, title: 'Volume'},
            {field: 'wholeQuantity', sort: true, title: 'Converted Quantity'},
            {field: 'carrierName', sort: true, title: 'Carrier'},
            {field: 'carnumber', sort: true, title: 'Vehicle Number'},
            {field: 'siji', sort: true, title: 'Driver'},
            {field: 'sijidianhua', sort: true, title: 'Driver Phone'},
            {field: 'zhunzailiang', sort: true, title: 'Capacity'},
            {field: 'fulllv', sort: true, title: 'Full Load Rate'},
            {field: 'createBy', sort: true, title: 'Creator'},
            {field: 'createTime', sort: true, title: 'Creation Time'},
            {field: 'memo', sort: true, title: 'Remark'},
            {align: 'center', toolbar: '#tableBar', title: 'Operation'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    OrderOutpreparePlatooncar.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(OrderOutpreparePlatooncar.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 导出excel按钮
     */
    OrderOutpreparePlatooncar.exportExcel = function () {
        var checkRows = table.checkStatus(OrderOutpreparePlatooncar.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("Please select data to export");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    OrderOutpreparePlatooncar.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/orderOutpreparePlatooncar/delete", function (data) {
                Feng.success("Delete successful!");
                table.reload(OrderOutpreparePlatooncar.tableId);
            }, function (data) {
                Feng.error("Delete failed!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("Are you sure to delete?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + OrderOutpreparePlatooncar.tableId,
        url: Feng.ctxPath + '/orderOutpreparePlatooncar/list',
        page: true,
        limit: 40,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: OrderOutpreparePlatooncar.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        OrderOutpreparePlatooncar.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        OrderOutpreparePlatooncar.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + OrderOutpreparePlatooncar.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'delete') {
            OrderOutpreparePlatooncar.onDeleteItem(data);
        }
    });
});
